<!--
 * @Author: your name
 * @Date: 2022-03-15 14:10:54
 * @LastEditTime: 2022-03-17 20:54:28
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\手写call220315.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写bind</title>
</head>

<body>
  <script>
    Function.prototype.mybind = function (target) {

      //target:改变返回函数执行时的this指向
      var obj = target || window;
      console.log(arguments,"arguments")
      var args = [].slice.call(arguments, 1); //获取bind时传入的绑定实参
      var args1 = [].slice.call(arguments, 0); //获取bind时传入的绑定实参
      console.log(args,"args")
      console.log(args1,"args1")
      var self = this; //要bind的函数
      console.log(self,"self")
      var _fn = function () {
        var _args = [].slice.call(arguments, 0); //新函数执行时传递的实际参数
        console.log(_args,"_args")
        return self.apply(obj, args.concat(_args));
      }
      return _fn
    }


    var name = 'fangfang';
    var obj = {
      name: 'geyao'
    };

    function fn(a, b, c) {
      console.log(a + b + c + this.name);
      return a + b + c + this.name
    };
    fn.mybind(obj,"我的","老婆","是")(); // 我的老婆是geyao
  </script>
</body>

</html>